-- 自动安装 Packer.nvim
-- 插件安装目录
-- ~/.local/share/nvim/lazy/lazy.nvim


-- https://github.com/folke/lazy.nvim
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
  vim.fn.system({
    "git",
    "clone",
    "--filter=blob:none",
    "https://github.com/folke/lazy.nvim.git",
    "--branch=stable", -- latest stable release
    lazypath,
  })
end
vim.opt.rtp:prepend(lazypath)

local status_ok, _ = pcall(require, "lazy")
if not status_ok then
    vim.notify("没有安装 lazy.nvim")
    return
end
require("lazy").setup("plugins")




local fn = vim.fn
local install_path = fn.stdpath("data") .. "/site/pack/packer/start/packer.nvim"
local packer_bootstrap
if fn.empty(fn.glob(install_path)) > 0 then
    vim.notify("正在安装Pakcer.nvim，请稍后...")
    packer_bootstrap =
        fn.system(
        {
            "git",
            "clone",
            "--depth",
            "1",
            "https://github.com/wbthomason/packer.nvim",
            install_path
        }
    )
    -- https://github.com/wbthomason/packer.nvim/issues/750
    local rtp_addition = vim.fn.stdpath("data") .. "/site/pack/*/start/*"
    if not string.find(vim.o.runtimepath, rtp_addition) then
        vim.o.runtimepath = rtp_addition .. "," .. vim.o.runtimepath
    end
    vim.notify("Pakcer.nvim 安装完毕")
end

-- Use a protected call so we don't error out on first use
local status_ok, packer = pcall(require, "packer")
if not status_ok then
    vim.notify("没有安装 packer.nvim")
    return
end

packer.startup(
    {
        function(use)
            --这里是安装插件的位置 
            -- Packer can manage itself
            use 'wbthomason/packer.nvim'
            -- https://github.com/folke/neodev.nvim
            use "folke/neodev.nvim"

            --[[ -- https://github.com/williamboman/nvim-lsp-installer
            use({"williamboman/nvim-lsp-installer"}) ]]

            use {
                "williamboman/mason.nvim",
                "williamboman/mason-lspconfig.nvim",
            }

            -- 主题
            use 'cormacrelf/vim-colors-github'
            use 'morhetz/gruvbox'
            use 'sainnhe/edge'
            use "ful1e5/onedark.nvim"
            use 'folke/tokyonight.nvim'

            -- 快捷键提示
            -- https://github.com/folke/which-key.nvim
            use 'folke/which-key.nvim'
            -- 启动界面
            -- https://github.com/glepnir/dashboard-nvim
            use 'glepnir/dashboard-nvim'
            -- 项目管理
            -- https://github.com/ahmedkhalf/project.nvim
            use "ahmedkhalf/project.nvim"
---------------------------------------
-- 窗口
---------------------------------------
            --[[ 
            use 'preservim/nerdtree'
            use 'Xuyuanp/nerdtree-git-plugin'
            ]]
            --[[ ## 文件浏览 ]]
            -- https://github.com/nvim-tree/nvim-tree.lua
            -- use {
            --     'nvim-tree/nvim-tree.lua',
            --     requires = {'nvim-tree/nvim-web-devicons', 'moll/vim-bbye',},
            --     tag = 'nightly' -- optional, updated every week. (see issue #1193)
            -- } 

            -- https://github.com/nvim-neo-tree/neo-tree.lua
            use {
                "nvim-neo-tree/neo-tree.nvim",
                branch = "v2.x",
                requires = {
                    "nvim-lua/plenary.nvim",
                    "nvim-tree/nvim-web-devicons", -- not strictly required, but recommended
                    "MunifTanjim/nui.nvim",
                }
            }

            -- ## window-picker
            -- https://github.com/s1n7ax/nvim-window-picker
            -- use {
            --    's1n7ax/nvim-window-picker',
            --    tag = 'v1.*',
            -- }


            -- ## bufferline
            -- https://github.com/akinsho/bufferline.nvim
            use {
                "akinsho/bufferline.nvim",
                requires = {"kyazdani42/nvim-web-devicons", "moll/vim-bbye"}
            }

            -- ## 状态栏
            -- https://github.com/nvim-lualine/lualine.nvim
            use {
                'nvim-lualine/lualine.nvim',
                requires = { 'kyazdani42/nvim-web-devicons', opt = true }
            }

            -- ## 终端
            use "akinsho/toggleterm.nvim"

            --[[ -- ## 大纲预览
            -- https://github.com/liuchengxu/vista.vim
            use "liuchengxu/vista.vim"
            ]]

            -- ## 大纲预览
            -- https://github.com/simrat39/symbols-outline.nvim
            use 'simrat39/symbols-outline.nvim'

            -- ## bufexplorer
            use 'jlanzarotta/bufexplorer'

            -- ## markdown-preview
            -- https://github.com/iamcco/markdown-preview.nvim
            -- use {
            --     "iamcco/markdown-preview.nvim",
            --     run = function() vim.fn["mkdp#util#install"]() end,
            -- }

---------------------------------------
-- 代码
---------------------------------------

            -- ## 错误美化
            -- https://github.com/folke/trouble.nvim
            use {
                "folke/trouble.nvim",
                requires = "kyazdani42/nvim-web-devicons",
            }

            --[[ -- ## 调试
            -- https://github.com/puremourning/vimspector
            use({"puremourning/vimspector"})
            ]]

            -- ## dap
            -- https://github.com/mfussenegger/nvim-dap
            use 'mfussenegger/nvim-dap'
            -- https://github.com/rcarriga/nvim-dap-ui
            use { "rcarriga/nvim-dap-ui", requires = {"mfussenegger/nvim-dap"} }
            -- https://github.com/leoluz/nvim-dap-go
            use 'leoluz/nvim-dap-go'

            -- ## goto-preview
            -- https://github.com/rmagatti/goto-preview
            use {
                "rmagatti/goto-preview",
            }

            -- ## todo-comments
            -- https://github.com/folke/todo-comments.nvim
            use {
                "folke/todo-comments.nvim",
                requires = "nvim-lua/plenary.nvim"
            }

---------------------------------------
-- git
---------------------------------------
            -- ## git
            -- https://github.com/kdheepak/lazygit.nvim
            use 'kdheepak/lazygit.nvim'
            use "lewis6991/gitsigns.nvim"


---------------------------------------
--  语法显示
---------------------------------------
            -- ## 方法高亮
            -- https://github.com/m-demare/hlargs.nvim
            use {
                "m-demare/hlargs.nvim",
                requires = "nvim-treesitter/nvim-treesitter"
            }

            -- ## 显示相同的单词
            -- https://github.com/RRethy/vim-illuminate
            use {
                "RRethy/vim-illuminate",
                requires = "nvim-treesitter/nvim-treesitter"
            }

            -- ## 方法聚焦, 集成在lualine状态栏中
            -- https://github.com/SmiteshP/nvim-gps
            -- use {
            --    "SmiteshP/nvim-gps",
            --    requires = "nvim-treesitter/nvim-treesitter",
            --    config = function()
            --        require("nvim-gps").setup()
            --        --  require('goto-preview').setup {}
            --    end
            -- }

            -- ## 当前方法吸顶
            -- https://github.com/nvim-treesitter/nvim-treesitter-context
            --use {
            --    "nvim-treesitter/nvim-treesitter-context",
            --    requires = "nvim-treesitter/nvim-treesitter"
            -- }

            -- ## 当前方法吸顶
            -- use {
            --    "SmiteshP/nvim-navic",
            --    requires = "neovim/nvim-lspconfig"
            -- }

            -- ## 语法高亮
            -- https://github.com/nvim-treesitter/nvim-treesitter
            use { 'nvim-treesitter/nvim-treesitter', run = ':TSUpdate' }

            -- ## 彩虹括号
            -- https://github.com/p00f/nvim-ts-rainbow
            use "p00f/nvim-ts-rainbow"

            -- ## 对齐
            -- https://github.com/lukas-reineke/indent-blankline.nvim
            use "lukas-reineke/indent-blankline.nvim"

            --[[ 
            -- ## 代码折叠 
            use {
                "anuvyklack/pretty-fold.nvim",
                config = function()
                    require("pretty-fold").setup {}
                    require("pretty-fold.preview").setup()
                end
            }
            ]]
            --[[ 
            -- ## 高亮目标行首字符
            -- use("unblevable/quick-scope")
            ]]
---------------------------------------
-- 快捷操作 
---------------------------------------
            -- ## 添加 "" () {}
            -- https://github.com/ur4ltz/surround.nvim
            -- use "ur4ltz/surround.nvim"
            -- https://vimawesome.com/plugin/surround-vim
            use "tpope/vim-surround"

            -- ## Comment 注释
            -- https://github.com/numToStr/Comment.nvim
            use"numToStr/Comment.nvim"

            -- ## tsx 注释增强
            -- https://github.com/JoosepAlviste/nvim-ts-context-commentstring
            -- use "JoosepAlviste/nvim-ts-context-commentstring"
            -- ## 自动关闭标签
            -- https://github.com/windwp/nvim-autopairs
            use "windwp/nvim-autopairs"

            -- ## tsx 自动生成关闭标签
            -- https://github.com/windwp/nvim-ts-autotag
            -- use "windwp/nvim-ts-autotag"
            -- ## 格式化
            -- https://github.com/sbdchd/neoformat
            use "sbdchd/neoformat"

            -- use{
            --     "AndrewRadev/switch.vim",
            --     event = {"BufRead", "BufNewFile"}
            -- }
            -- ## 多光标操作
            -- https://github.com/mg979/vim-visual-multi
            use "mg979/vim-visual-multi"

            -- ## 跳转插件 easy motion
            -- https://github.com/phaazon/hop.nvim
            use {
                "phaazon/hop.nvim",
                branch = "v1", -- optional but strongly recommended
                config = function()
                    -- you can configure Hop the way you like here; see :h hop-config
                    require "hop".setup {keys = "etovxqpdygfblzhckisuran"}
                end
            }


---------------------------------------
-- 搜索和替换
---------------------------------------
            -- ## 文件搜索
            -- https://github.com/nvim-telescope/telescope.nvim
            use {
                'nvim-telescope/telescope.nvim',
                requires = { {'nvim-lua/plenary.nvim'} }
            }
            -- https://github.com/LinArcX/telescope-env.nvim
            use "LinArcX/telescope-env.nvim"
            -- https://github.com/nvim-telescope/telescope-ui-select.nvim
            use "nvim-telescope/telescope-ui-select.nvim"

            -- 文本查找和替换
            -- https://github.com/nvim-pack/nvim-spectre
            use "windwp/nvim-spectre"

---------------------------------------
-- LSP 
---------------------------------------
            -- https://github.com/neovim/nvim-lspconfig
            -- https://github.com/neovim/nvim-lspconfig/blob/master/doc/server_configurations.md
            -- lsp 加载进度ui
            use {"j-hui/fidget.nvim", tag = 'legacy'}
            -- Configurations for Nvim LSP
            use 'neovim/nvim-lspconfig'
            -- nvim-cmp
            use 'hrsh7th/nvim-cmp'
            -- ## Snippet 引擎
            -- https://github.com/hrsh7th/vim-vsnip
            use 'hrsh7th/vim-vsnip'
            -- ## 补全源
            use 'hrsh7th/cmp-vsnip'    -- { name = 'vsnip' }
            use 'hrsh7th/cmp-nvim-lsp' -- { name = nvim_lsp }
            use 'hrsh7th/cmp-buffer'   -- { name = 'buffer' },
            use 'hrsh7th/cmp-path'     -- { name = 'path' }
            use 'hrsh7th/cmp-cmdline'  -- { name = 'cmdline' }
            use "hrsh7th/cmp-nvim-lsp-signature-help" -- { name = 'nvim_lsp_signature_help' }
            -- ## 拼写建议
            use("f3fora/cmp-spell")
            -- ## 常见编程语言代码段
            -- https://github.com/rafamadriz/friendly-snippets
            use 'rafamadriz/friendly-snippets'
            -- ## UI 增强
            -- https://github.com/onsails/lspkind-nvim
            use 'onsails/lspkind-nvim'
            -- https://github.com/tami5/lspsaga.nvim
            use "tami5/lspsaga.nvim"
            -- https://github.com/jose-elias-alvarez/null-ls.nvim
            use {"jose-elias-alvarez/null-ls.nvim", requires = "nvim-lua/plenary.nvim"}
            -- ## TypeScript 增强
            use {"jose-elias-alvarez/nvim-lsp-ts-utils", requires = "nvim-lua/plenary.nvim"}
            -- ## Lua 增强
            use "folke/lua-dev.nvim"
            -- ## JSON 增强
            -- https://github.com/b0o/schemastore.nvim
            use "b0o/schemastore.nvim"
            -- ## Rust 增强
            use "simrat39/rust-tools.nvim"
            -- ## go 语法工具
            -- https://github.com/fatih/vim-go
            use "fatih/vim-go"

---------------------------------------
-- 不知道是什么 
---------------------------------------

            -- ## 执行代码片段
            -- https://github.com/michaelb/sniprun
            use {"michaelb/sniprun", run = "bash ./install.sh"}
            -- ## 截图
            use {"kristijanhusak/vim-carbon-now-sh", cmd = "CarbonNowSh"}
            -- https://github.com/nvim-lua/plenary.nvim
            use "nvim-lua/plenary.nvim"
            --[[ 
            -- ## 精美弹窗
            -- https://github.com/rcarriga/nvim-notify
            use {
                "rcarriga/nvim-notify",
                config = function()
                    require("custom.plugins.notify")
                end
            }
             ]]
            --[[ 
            -- ## 自动恢复光标位置
            use {
                "ethanholz/nvim-lastplace",
                config = function()
                    --   require("conf.nvim-lastplace")
                    require("custom.plugins.lastplace")
                end
            }
            ]]
            if packer_bootstrap then
                packer.sync()
            end
        end,
        config = {
            -- 最大并发数
            max_jobs = 16,
            -- 自定义源
            git = {
                --default_url_format = "https://mirror.ghproxy.com/https://github.com/%s"
            },
            display = {
                -- 使用浮动窗口显示
                open_fn = function()
                    return require("packer.util").float({ border = "single" })
                end,
            },
        }
    }
)


-- 每次保存 plugins.lua 自动安装插件
-- move to autocmds.lua
-- pcall(
--   vim.cmd,
--   [[
-- augroup packer_user_config
-- autocmd!
-- autocmd BufWritePost plugins.lua source <afile> | PackerSync
-- augroup end
-- ]]
-- )

-- 插件配置
-- require("plugin.nvim-tree")
-- require("plugin.alpha")
-- require("plugin.vista")
require("plugin.neo-tree")
-- require("plugin.window-picker")
require("plugin.bufferline")
require("plugin.lualine")
require("plugin.telescope")
require("plugin.dashboard")
require("plugin.project")
require("plugin.indent-blankline")
require("plugin.toggleterm")
-- require("plugin.surround")
require("plugin.comment")
require("plugin.nvim-autopairs")
require("plugin.fidget")
require("plugin.neoformat")
require("plugin.todo-comments")
require("plugin.vim-visual-multi")
require("plugin.nvim-spectre")
require("plugin.vim-illuminate")
require("plugin.symbols-outline")
require("plugin.hlargs")
require("plugin.which-key")
require("plugin.hop")
require("plugin.gitsigns")
-- require("plugin.nvim-gps")
-- require("plugin.nvim-navic")
require("plugin.trouble")
require("plugin.goto-preview")
require("plugin.vsnip")
-- require("plugin.treesitter-context")
require("plugin.vimspector")
-- treesitter
require("plugin.treesitter")
-- mason
require("plugin.mason")
-- 调试(DAP)
require("plugin.dap")
-- LSP
require("plugin.lsp")
require("plugin.lsp-cmp")
require("plugin.lsp-ui")
-- require("plugin.lsp-formatter")
-- linter & formatter
require("plugin.null-ls")
-- user define
require('plugin.colorscheme')

